<?php
include_once("core/mapped.php");

/**
 * Door
 *
 * ModelClass doorped with door table to deal with a single door
 *
 * @copyright joel Simoes
 * @package models_doors
 */
class Door extends Mapped {
	public $table = "door";


	public function delete(){
		parent::delete();
		$doors = new Doors();
		$query = "UPDATE `door` set toid = 0 WHERE toid = {$this->field->id}";
		$this->database->queries($query);
	}

	/**
	 * Adds an user with request's vars
	 */
	public function addDoor() {
		$tab = Array(
        "id"  => "id",
        "map_id" => "map_id",
        "point" => "point",
        "toid" => "toid"
        );
        $this->fromRequest($tab);
	}

	//not used i think
	public function getXML(){
		$tab = Array(
       	"id"  => "did",
        "map_id" => "dmap_id",
        "point" => "dpoint",
        "toid" => "dtoid"
        );
        return $this->toXML("door",$tab);
	}

}

/**
 * Users
 *
 * Class dealing with a group of Doors (instances of ModelClass_Door)
 *
 * @copyright joel Simoes
 * @version 01
 * @package models_doors
 */
class Doors extends Normal{

	/**
	 * Retrieves the list of Doors
	 * @return $DoorsNode the node containing all doors
	 */
	public function getDoors() {
		$DoorsNode = $this->response->doc->createElement("doors");

		$req       = "SELECT * FROM `door`;";
		$result    = $this->database->queries($req);

		while($obj = $result->fetch_object()) {
			$DoorNode = null;
			$obj->toid = round($obj->toid);
			$tab = array(
			"id"  => "id",
			"map_id" => "map_id",
			"point" => "point",
			"toid" => "toid"
			);
			$DoorNode = $this->response->mapObjectXML($obj, "door", $tab);
			$DoorsNode->appendChild($DoorNode);
		}

		return $DoorsNode;
	}

	/**
	 * Retrieves a range of the list of Doors
	 * @param string filter (sql filter)
	 * @return DOMElement $DoorsNode
	 */
	public function getDoorsComplex($sql, $return = "xml") {
		$arrayReturn = array();
		$DoorsNode = $this->response->doc->createElement("doors");

		$req       = "SELECT * FROM `door` ".$sql.";";
		$result    = $this->database->queries($req);

		if($result != FALSE){
			while($obj = $result->fetch_object()) {
				$obj->toid = round($obj->toid);
				$DoorNode = null;
				$tab = array( 	"id"  => "id",
				"map_id" => "map_id",
				"point" => "point",
				"toid" => "toid");
				$arrayReturn[] = $obj;
				$DoorNode = $this->response->mapObjectXML($obj, "door", $tab);
				$DoorsNode->appendChild($DoorNode);
			}
		}
		if ($return == "array") return $arrayReturn;
		return $DoorsNode;
	}

	/**
	 * Returns the number of users filtered
	 * @param string sql filter
	 * @return the number of users
	 */
	public function getNumDoors($sql){
		$sqlCount = "SELECT COUNT(*) FROM `door` ".$sql;
		$result   = $this->database->queries($sqlCount);
		$array    = $result->fetch_array();

		return $array[0];
	}
}

?>
